package com.lly.songhigh.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lly.songhigh.R;
import com.lly.songhigh.constant.StringResouce;
import com.lly.songhigh.entity.RankingList;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by dllo on 15/10/23.
 */
public class ChartsListViewAdapter extends BaseAdapter {

    private Context context;
    private List<RankingList> rankingLists;
    private String name;
    private List<RankingList.RefsEntity> refsEntityLists;

    public ChartsListViewAdapter(Context context, List<RankingList> rankingLists, String name) {
        this.context = context;
        this.rankingLists = rankingLists;
        this.name = name;
        if (name.equals(StringResouce.GOVERNMENT)) {
            refsEntityLists = rankingLists.get(0).getRefs();
        }
        if (name.equals(StringResouce.SPECIAL)) {
            refsEntityLists = rankingLists.get(1).getRefs();
        }
        if (name.equals(StringResouce.WORLD)) {
            refsEntityLists = rankingLists.get(2).getRefs();
        }
    }

    @Override
    public int getCount() {
        return refsEntityLists != null && refsEntityLists.size() > 0 ? refsEntityLists.size() : 0;
    }

    @Override
    public RankingList.RefsEntity getItem(int position) {
        return refsEntityLists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.charts_list_view_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        RankingList.RefsEntity refsEntity = getItem(position);
        if (refsEntity.getSongs().size() == 3) {
            String frist = "1." + refsEntity.getSongs().get(0).getName() + "-"
                    + refsEntity.getSongs().get(0).getSingerName();
            viewHolder.rankingListFirst.setText(frist);
            String second = "2." + refsEntity.getSongs().get(1).getName() + "-"
                    + refsEntity.getSongs().get(1).getSingerName();
            viewHolder.rankingListSecond.setText(second);
            String third = "3." + refsEntity.getSongs().get(2).getName() + "-"
                    + refsEntity.getSongs().get(2).getSingerName();
            viewHolder.rankingListThird.setText(third);
            viewHolder.rankingListTitle.setText(refsEntity.getTitle());
            Picasso.with(context).load(refsEntity.getImage().getPic()).placeholder(R.mipmap.placeholder_disk_300)
                    .error(R.mipmap.placeholder_disk_300).into(viewHolder.rankingListPicture);

        } else {

            String frist = "1." + refsEntity.getSongs().get(0).getName() + "-"
                    + refsEntity.getSongs().get(0).getSingerName();
            viewHolder.rankingListFirst.setText(frist);
            viewHolder.rankingListTitle.setText(refsEntity.getTitle());
            Picasso.with(context).load(refsEntity.getImage().getPic()).placeholder(R.mipmap.placeholder_disk_300)
                    .error(R.mipmap.placeholder_disk_300).into(viewHolder.rankingListPicture);

        }
        return convertView;

    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'charts_list_view_item.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @Bind(R.id.rankingList_picture)
        ImageView rankingListPicture;
        @Bind(R.id.rankingList_title)
        TextView rankingListTitle;
        @Bind(R.id.rankingList_first)
        TextView rankingListFirst;
        @Bind(R.id.rankingList_second)
        TextView rankingListSecond;
        @Bind(R.id.rankingList_third)
        TextView rankingListThird;
        @Bind(R.id.rankingList_linear)
        LinearLayout rankingListLinear;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

}

